package cn.team11.smarteye.widget;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;

import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IMeasurablePagerTitleView;

/**
 * Created by Murphy on 2017/11/7.
 */

public class CustomPagerTitleView extends android.support.v7.widget.AppCompatTextView implements IMeasurablePagerTitleView{


    protected int mSelectedColor;
    protected int mNormalColor;

    public CustomPagerTitleView(Context context) {
        super(context, (AttributeSet)null);
        init(context);
    }

    private void init(Context context) {
        this.setGravity(17);
        int padding = UIUtil.dip2px(context, 10.0D);
        this.setPadding(padding, 0, padding, 0);
        this.setSingleLine();
        this.setEllipsize(TextUtils.TruncateAt.END);
    }


    @Override
    public int getContentLeft() {
        Rect bound = new Rect();
        this.getPaint().getTextBounds(this.getText().toString(), 0, this.getText().length(), bound);
        int contentWidth = bound.width();
        return this.getLeft() + this.getWidth() / 2 - contentWidth / 2;
    }

    @Override
    public int getContentTop() {
        Paint.FontMetrics metrics = this.getPaint().getFontMetrics();
        float contentHeight = metrics.bottom - metrics.top;
        return (int)((float)(this.getHeight() / 2) - contentHeight / 2.0F);
    }

    @Override
    public int getContentRight() {
        Rect bound = new Rect();
        this.getPaint().getTextBounds(this.getText().toString(), 0, this.getText().length(), bound);
        int contentWidth = bound.width();
        return this.getLeft() + this.getWidth() / 2 + contentWidth / 2;
    }

    @Override
    public int getContentBottom() {
        Paint.FontMetrics metrics = this.getPaint().getFontMetrics();
        float contentHeight = metrics.bottom - metrics.top;
        return (int)((float)(this.getHeight() / 2) + contentHeight / 2.0F);
    }

    @Override
    public void onSelected(int i, int i1) {
        this.setTextColor(this.mSelectedColor);
        this.setSelected(true);
    }

    @Override
    public void onDeselected(int i, int i1) {
        this.setTextColor(this.mNormalColor);
        this.setSelected(false);
    }

    @Override
    public void onLeave(int i, int i1, float v, boolean b) {

    }

    @Override
    public void onEnter(int i, int i1, float v, boolean b) {

    }
    public int getSelectedColor() {
        return this.mSelectedColor;
    }

    public void setSelectedColor(int selectedColor) {
        this.mSelectedColor = selectedColor;
    }

    public int getNormalColor() {
        return this.mNormalColor;
    }

    public void setNormalColor(int normalColor) {
        this.mNormalColor = normalColor;
    }
}
